package com.muyu.payment.service.impl;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.payment.domain.PaymentDict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.muyu.payment.mapper.PaymentOrderMapper;
import com.muyu.payment.domain.PaymentOrder;
import com.muyu.payment.domain.model.PaymentOrderQueryModel;
import com.muyu.payment.domain.model.PaymentOrderPageQueryModel;
import com.muyu.payment.domain.model.PaymentOrderSaveModel;
import com.muyu.payment.domain.model.PaymentOrderEditModel;
import com.muyu.payment.service.PaymentOrderService;
import org.springframework.transaction.annotation.Transactional;
/**
 * @author <a href="mailto:3546903628@qq.com">w1102</a>
 * @version 1.0
 * @description:
 * @date 2025/9/18 11:58
 */
@Slf4j
@Service
public class PaymentOrderServiceImpl extends ServiceImpl<PaymentOrderMapper, PaymentOrder> implements PaymentOrderService {
    @Autowired
    private PaymentOrderMapper paymentOrderMapper;

    /**
     * 查询支付订单列表
     *
     * @param pageQueryModel 支付订单
     * @return 支付订单
     */
    @Override
    public PageQueryModel<PaymentOrderPageQueryModel> pageQuery(PaymentOrderQueryModel pageQueryModel) {
        Page<PaymentOrderPageQueryModel> page = new Page<>(pageQueryModel.getPageNum(), pageQueryModel.getPageSize());
        Page<PaymentOrderPageQueryModel> resultPage = paymentOrderMapper.pageQuery(page, pageQueryModel);
        return PageQueryModel.of(resultPage);
    }

    /**
     * 查询支付订单列表
     * @param pageQueryModel 支付订单
     * @return 支付订单
     */
    @Override
    public List<PaymentOrder> exportList(PaymentOrder pageQueryModel) {
        LambdaQueryWrapper<PaymentOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getOrderNo()), PaymentOrder::getOrderNo, pageQueryModel.getOrderNo());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getUserId()), PaymentOrder::getUserId, pageQueryModel.getUserId());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getAmount()), PaymentOrder::getAmount, pageQueryModel.getAmount());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getOrderStatus()), PaymentOrder::getOrderStatus, pageQueryModel.getOrderStatus());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getPayTime()), PaymentOrder::getPayTime, pageQueryModel.getPayTime());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getPayChannel()), PaymentOrder::getPayChannel, pageQueryModel.getPayChannel());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getPayTradeNo()), PaymentOrder::getPayTradeNo, pageQueryModel.getPayTradeNo());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getSubject()), PaymentOrder::getSubject, pageQueryModel.getSubject());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getBody()), PaymentOrder::getBody, pageQueryModel.getBody());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getClientIp()), PaymentOrder::getClientIp, pageQueryModel.getClientIp());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getNotifyUrl()), PaymentOrder::getNotifyUrl, pageQueryModel.getNotifyUrl());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getReturnUrl()), PaymentOrder::getReturnUrl, pageQueryModel.getReturnUrl());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getExpireTime()), PaymentOrder::getExpireTime, pageQueryModel.getExpireTime());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getRemark()), PaymentOrder::getRemark, pageQueryModel.getRemark());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getPayType()), PaymentOrder::getPayType, pageQueryModel.getPayType());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getCreateTime()), PaymentOrder::getCreateTime, pageQueryModel.getCreateTime());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getUpdateBy()), PaymentOrder::getUpdateBy, pageQueryModel.getUpdateBy());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryModel.getUpdateTime()), PaymentOrder::getUpdateTime, pageQueryModel.getUpdateTime());
        queryWrapper.orderByDesc(PaymentOrder::getCreateTime);
        return this.list(queryWrapper);
    }

    /**
     * 支付订单添加
     *
     * @param saveModel 支付订单
     */
    @Override
    public void save(PaymentOrderSaveModel saveModel) {
        PaymentOrder paymentOrder = PaymentOrder.builder()
                .orderNo(saveModel.getOrderNo())
                .userId(saveModel.getUserId())
                .amount(saveModel.getAmount())
                .orderStatus(saveModel.getOrderStatus())
                .payTime(saveModel.getPayTime())
                .payChannel(saveModel.getPayChannel())
                .payTradeNo(saveModel.getPayTradeNo())
                .subject(saveModel.getSubject())
                .body(saveModel.getBody())
                .clientIp(saveModel.getClientIp())
                .notifyUrl(saveModel.getNotifyUrl())
                .returnUrl(saveModel.getReturnUrl())
                .expireTime(saveModel.getExpireTime())
                .remark(saveModel.getRemark())
                .payType(saveModel.getPayType())
                .build();
        this.save(paymentOrder);
    }

    /**
     * 支付订单修改
     *
     * @param editModel 支付订单
     */
    @Override
    public void edit(PaymentOrderEditModel editModel) {
        PaymentOrder paymentOrder = PaymentOrder.builder()
                .id(editModel.getId())
                .orderNo(editModel.getOrderNo())
                .userId(editModel.getUserId())
                .amount(editModel.getAmount())
                .orderStatus(editModel.getOrderStatus())
                .payTime(editModel.getPayTime())
                .payChannel(editModel.getPayChannel())
                .payTradeNo(editModel.getPayTradeNo())
                .subject(editModel.getSubject())
                .body(editModel.getBody())
                .clientIp(editModel.getClientIp())
                .notifyUrl(editModel.getNotifyUrl())
                .returnUrl(editModel.getReturnUrl())
                .expireTime(editModel.getExpireTime())
                .remark(editModel.getRemark())
                .payType(editModel.getPayType())
                .build();
        this.updateById(paymentOrder);
    }

    /**
     * 若订单为待支付(1)，则更新为已支付(2)，写入三方交易号、支付渠道与支付类型。
     * 返回是否成功从待支付更新为已支付（幂等）。
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean markPaidIfPending(String orderNo, String thirdTradeNo, String payChannel, Long payType) {
        int rows = baseMapper.markPaidIfPending(orderNo, thirdTradeNo, payChannel, payType);
        return rows > 0;
    }

    /**
     * 订单是否已支付
     * @param orderNo
     * @return
     */
    @Override
    public boolean isPaid(String orderNo) {
        return baseMapper.isPaid(orderNo);
    }

    /**
     * 字典表
     * @param dictType
     * @return
     */
    @Override
    public List<PaymentDict> dict(String dictType) {
        List<PaymentDict> dictList = baseMapper.dict(dictType);
        return dictList;
    }

    /**
     * 根据订单号查询订单
     * @param orderNo 订单号
     * @return 订单信息
     */
    @Override
    public PaymentOrder getByOrderNo(String orderNo) {
        return this.getOne(
                new LambdaQueryWrapper<PaymentOrder>()
                        .eq(PaymentOrder::getOrderNo, orderNo)
        );
    }

}
